package cz.data.domain.standard.controller;

import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.standard.mapstruct.ContrastMapper;
import cz.data.domain.standard.model.dto.ContrastDto;
import cz.data.domain.standard.model.entity.ContrastEntity;
import cz.data.domain.standard.model.query.ContrastQuery;
import cz.data.domain.standard.model.vo.ContrastStatisticVo;
import cz.data.domain.standard.model.vo.ContrastTreeVo;
import cz.data.domain.standard.model.vo.ContrastVo;
import cz.data.domain.standard.service.ContrastService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"对照表"})
@RestController
@RequestMapping("/contrasts")
public class ContrastController extends BaseController {

    @Autowired
    private ContrastService contrastService;

    @Autowired
    private ContrastMapper contrastMapper;

    @ApiOperation(value = "对照表-详情", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<ContrastVo> getContrastById(@PathVariable String id) {
        ContrastEntity contrastEntity = contrastService.getContrastById(id);
        return new SimpleDataResponse<>(contrastMapper.toVO(contrastEntity));
    }

    @ApiOperation(value = "对照表-分页查询")
    @ApiImplicitParam(name = "contrastQuery", value = "查询实体contrastQuery", required = true, dataTypeClass = ContrastQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<ContrastVo>> getContrastPage(ContrastQuery contrastQuery) {
        QueryWrapper<ContrastEntity> queryWrapper = new QueryWrapper<>();
        IPage<ContrastEntity> page = contrastService.page(new Page<>(contrastQuery.getPageNum(), contrastQuery.getPageSize()), queryWrapper);
        List<ContrastVo> collect = page.getRecords().stream().map(contrastMapper::toVO).collect(Collectors.toList());
        JsonPage<ContrastVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "对照表-新增")
    @ApiImplicitParam(name = "contrast", value = "详细实体contrast", required = true, dataType = "ContrastDto")
    @PostMapping()
    public SimpleDataResponse<ContrastVo> saveContrast(@RequestBody @Validated({ValidationGroups.Insert.class}) ContrastDto contrast) {
        ContrastEntity contrastEntity = contrastService.saveContrast(contrast);
        return new SimpleDataResponse<>(contrastMapper.toVO(contrastEntity));
    }

    @ApiOperation(value = "对照表-修改", notes = "根据url的id来指定修改对象，并根据传过来的信息来修改详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "contrast", value = "详细实体contrast", required = true, dataType = "ContrastDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<ContrastVo> updateContrast(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) ContrastDto contrast) {
        ContrastEntity contrastEntity = contrastService.updateContrast(contrast);
        return new SimpleDataResponse<>(contrastMapper.toVO(contrastEntity));
    }

    @ApiOperation(value = "对照表-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteContrastById(@PathVariable String id) {
        contrastService.deleteContrastById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "对照表-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteContrastBatch(@PathVariable List<String> ids) {
        contrastService.deleteContrastBatch(ids);
        return BaseResponse.success();
    }

    @ApiOperation(value = "对照表-树结构")
    @GetMapping("/tree")
    public TableDataResponse<ContrastTreeVo> getContrastTree() {
        List<ContrastTreeVo> list = contrastService.getContrastTree();
        return new TableDataResponse<>(list);
    }

    @ApiOperation(value = "对照表-分页查询")
    @ApiImplicitParam(name = "contrastQuery", value = "查询实体contrastQuery", required = true, dataTypeClass = ContrastQuery.class)
    @GetMapping("/stat")
    public SimpleDataResponse<JsonPage<ContrastStatisticVo>> contrastStat(ContrastQuery contrastQuery) {
        QueryWrapper<ContrastEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(contrastQuery.getSourceName()), "c.source_name", contrastQuery.getSourceName());
        queryWrapper.like(StringUtils.isNotBlank(contrastQuery.getTableName()), "c.table_name", contrastQuery.getTableName());
        queryWrapper.like(StringUtils.isNotBlank(contrastQuery.getColumnName()), "c.column_name", contrastQuery.getColumnName());
        IPage<ContrastEntity> page = contrastService.statistic(new Page<>(contrastQuery.getPageNum(), contrastQuery.getPageSize()), queryWrapper);
        List<ContrastStatisticVo> collect = page.getRecords().stream().map(contrastMapper::toSVO).collect(Collectors.toList());
        JsonPage<ContrastStatisticVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }
}
